﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FizzWare.NBuilder;
using ar.com.juanpabloibanez.Samples.Models;
using ar.com.juanpabloibanez.Samples.ViewModels;

namespace ar.com.juanpabloibanez.Samples.Controllers
{
    public class ProductController : Controller
    {
        private static IList<Product> products;

        public ProductController()
        {
            if (products == null)
            {
                products = Builder<Product>.CreateListOfSize(1000).Build();
            }
        }

        public ActionResult Index(SearchProducViewModel searchProducViewModel)
        {
            ViewBag.EnhancePage = true;
            ViewBag.Action = "Index";

            DoIndex(searchProducViewModel);

            if (Request.IsAjaxRequest())
            {
                return PartialView("ProductsGrid", searchProducViewModel);
            }
            else
            {
                return View("Index",searchProducViewModel);    
            }
        }

        private void DoIndex(SearchProducViewModel searchProducViewModel)
        {
            if (searchProducViewModel.Delete > 0)
            {
                Product product = products.Where(x => x.Id == searchProducViewModel.Delete).Single();
                products.Remove(product);
            }

            var filteredProducts = from p in products
                                   where p.Active == searchProducViewModel.Active
                                   && (searchProducViewModel.Name == null ||
                                       p.Name.Contains(searchProducViewModel.Name))
                                   select p;

            searchProducViewModel.PagerViewModel.TotalItems = filteredProducts.Count();

            searchProducViewModel.ListOfProducts = filteredProducts
                .Skip(searchProducViewModel.PagerViewModel.Skip)
                .Take(searchProducViewModel.PagerViewModel.Take);

        }

        public ActionResult IndexNoJs(SearchProducViewModel searchProducViewModel)
        {
            ViewBag.EnhacePage = false;
            ViewBag.Action = "IndexNoJs";

            DoIndex(searchProducViewModel);

            return View("Index", searchProducViewModel);
        }

        //public ActionResult Page(SearchProducViewModel searchProducView)
        //{
        //    return Index(searchProducView);
        //}

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
